/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.biblioteca.controller;

import br.com.biblioteca.dao.DAOFacade;
import br.com.biblioteca.dao.IUserDAO;
import br.com.biblioteca.exceptions.BadRequestException;
import br.com.biblioteca.utils.ServletUtils;
import br.com.biblioteca.vo.User;
import java.util.Date;
import java.util.GregorianCalendar;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author joaosavio
 */
public class UserService implements IService {

    public void execute(ServiceRequestTO serviceRequestTO) throws Exception {
        if (serviceRequestTO.getAction().equals("update")) {
            update(serviceRequestTO.getRequest(), serviceRequestTO.getResponse());
            return;
        }
        if (serviceRequestTO.getAction().equals("login"))  {
            login(serviceRequestTO.getRequest(), serviceRequestTO.getResponse());
            return;
        }
        if (serviceRequestTO.getAction().equals("logout"))  {
            logout(serviceRequestTO.getRequest(), serviceRequestTO.getResponse());
            return;
        }

        throw new BadRequestException("Unknown action");
    }

    private void update(HttpServletRequest request, HttpServletResponse response) {
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute("user");
        user.setPassword(request.getParameter("password"));
        System.out.println(user.getLogin());
        user.setLastPasswordDateUpdated(new Date());
        IUserDAO userDAO = (IUserDAO) DAOFacade.getDAO(User.class);
        userDAO.update(user);
        //session.setAttribute("user", user);
    }

    private void login(HttpServletRequest request, HttpServletResponse response) {
        IUserDAO userDAO = (IUserDAO) DAOFacade.getDAO(User.class);
        String login = request.getParameter("login");
        String password = request.getParameter("password");
        User user = userDAO.get(login, password);
        if (user == null) {
            ServletUtils.dispatcher(request, response, Page.INDEX);
        }
        HttpSession session = request.getSession(true);
        session.setAttribute("user", user);
        Date date = user.getLastPasswordDateUpdated();
        GregorianCalendar calendar = new GregorianCalendar();
        calendar.setGregorianChange(date);
        calendar.add(GregorianCalendar.MONTH, 1);
        Date datePlusOneMonth = calendar.getTime();
        Date today = new Date();
        System.out.println("Today: "+today);
        System.out.println("User password last change: "+user.getLastPasswordDateUpdated());
        System.out.println("Deadline: "+datePlusOneMonth);
        if (today.getTime() > datePlusOneMonth.getTime()) {
            ServletUtils.dispatcher(request, response, Page.UPDATE_USER);
        }

        ServletUtils.dispatcher(request, response, Page.MAIN);
    }

    private void logout(HttpServletRequest request, HttpServletResponse response) {
        HttpSession session = request.getSession(false);
        session.invalidate();
        ServletUtils.dispatcher(request, response, Page.INDEX);
    }
}
